#include "stdafx.h"
#include "GameObject.h"

void GameObject::move(float x, float y, float z) {
	m_node->translate(x,y,z,Ogre::Node::TS_LOCAL);
}

GameObject::GameObject() {
	m_node = NULL;
	m_movableObjects.clear();
	m_dead = false;
	m_physicsComponent = NULL;
}

void GameObject::setSceneNode(Ogre::SceneNode *node) {
	m_node = node;
}

std::list<Ogre::MovableObject*> GameObject::getMovableObjects() {
	return m_movableObjects;
}

void GameObject::destroyAllMovableObjects(Ogre::SceneManager *sceneMgr) {
	for (std::list<Ogre::MovableObject*>::const_iterator ci = m_movableObjects.begin(); ci != m_movableObjects.end(); ++ci)	{
		sceneMgr->destroyMovableObject(*ci);
	}
}

void GameObject::setOrientation(Ogre::Quaternion orientation) {
	if(m_node) {
		m_node->setOrientation(orientation);
	}
}

void GameObject::synchPositionAndOrientation(Ogre::Vector3 position, Ogre::Quaternion orientation) {
	setPosition(position);
	setOrientation(orientation);
	if(m_physicsComponent) {
		m_physicsComponent->synchPositionAndOrientation(position, orientation);
	}
}

void GameObject::addMovableObject(Ogre::MovableObject* movableObject) {
	m_movableObjects.push_back(movableObject);	
}

void GameObject::kill() {
	m_dead = true;
}

bool GameObject::isDead() {
	return m_dead;
}

void GameObject::setID(unsigned int newID) {
	m_ID = newID;
}

PhysicsComponent* GameObject::getPhysicsComponent() {
	return m_physicsComponent;
}

unsigned int GameObject::getID() {
	return m_ID;
}

Ogre::Vector3 GameObject::getPosition(Ogre::Node* node) {
	if (node->getParent() == NULL) {
		return node->getPosition();
	} else {
		return node->getPosition() + getPosition(node->getParent());
	}
}

void GameObject::setPosition(Ogre::Vector3 pos) {
	if(m_node) {
		m_node->setPosition(pos);
	}
}

GameObject::~GameObject() {
	if (m_node != NULL) {
		m_node->removeAndDestroyAllChildren();
		m_node = NULL;
	}
}

Ogre::SceneNode* GameObject::getSceneNode() {
	return m_node;
}

Ogre::SceneNode* GameObject::getCameraNode() {
	return m_node;
}